//
//  BeaconTableViewController.m
//  mpoi
//
//  Created by thomasho on 15/11/9.
//  Copyright © 2015年 thomasho. All rights reserved.
//

#import "BeaconTableViewController.h"
#import <BrightSDK/BrtbeaconSDK.h>
#import "Foundation_defines.h"
#import "DBData.h"
#import "NSDictionary+expanded.h"
@interface BeaconTableViewController ()

@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation BeaconTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [BRTBeaconSDK startRangingBeaconsInRegions:nil onCompletion:^(NSArray *beacons, BRTBeaconRegion *region, NSError *error) {
        [self reloadData:beacons];
    }];
}

- (void)reloadData:(NSArray *)beacons {
    self.dataArray = beacons;
    [self.tableView reloadData];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"list" forIndexPath:indexPath];
    BRTBeacon *beacon = self.dataArray[indexPath.row];
    //to do
    cell.textLabel.text = beacon.name;
    cell.detailTextLabel.text = SF2(@"%@,%@",beacon.major,beacon.minor);
    if ([[self.dataDict valueForJSONStrKey:@"beacon_id"] isEqualToString:beacon.major]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BRTBeacon *beacon = self.dataArray[indexPath.row];

    [self.navigationController popViewControllerAnimated:YES];
}
@end
